<template>
  <div class="login-container">
    <div class="login-box">
      <h2 class="login-title">心智守护哨系统登录</h2>
      <el-form ref="form" :model="form" :rules="rules">
        <el-form-item prop="username">
          <el-input
            v-model="form.username"
            placeholder="请输入用户名"
            prefix-icon="el-icon-user"
            @keyup.enter.native="handleLogin"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="form.password"
            type="password"
            placeholder="请输入密码"
            prefix-icon="el-icon-lock"
            show-password
            @keyup.enter.native="handleLogin"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            @click="handleLogin"
            :loading="loading"
            style="width: 100%"
          >登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { mapActions } from 'vuex'

export default {
  name: 'Login',
  data() {
    return {
      form: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ]
      },
      loading: false,
      showDefaultTip: true
    }
  },
  created() {
    // 如果已登录，直接跳转到首页
    if (this.$store.getters['auth/isAuthenticated']) {
      this.$router.push(this.$route.query.redirect || '/home')
    }
  },
  methods: {
    ...mapActions('auth', ['login']),

    handleLogin() {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.loading = true
          this.login({ username: this.form.username, password: this.form.password })
            .then(response => {
              if (response.token) {
                this.$message.success('登录成功')
                const user = response.data || {};
                if (user.role === '5' || user.role === 5) {
                  this.$router.replace('/questionnaire-processing');
                } else {
                  const redirect = this.$route.query.redirect || '/home'
                  this.$router.push(redirect)
                }
              } else {
                this.$message.error('登录失败，请检查用户名和密码')
              }
            })
            .catch(error => {
              this.$message.error(error.message || '登录失败，请检查用户名和密码')
            })
            .finally(() => {
              this.loading = false
            })
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.login-container {
  width: 100%;
  height: 100vh;
  background: url('../assets/pageBg.png') no-repeat center center;
  background-size: cover;
  display: flex;
  justify-content: center;
  align-items: center;

  .login-box {
    width: 400px;
    padding: 40px;
    background: rgba(0, 19, 47, 0.7);
    border-radius: 10px;
    box-shadow: 0 0 20px rgba(0, 0, 0, 0.3);
    border: 1px solid rgba(104, 216, 254, 0.5);

    .login-title {
      text-align: center;
      margin-bottom: 30px;
      color: #d3d6dd;
      font-size: 24px;
      text-shadow: 0 0 10px rgba(104, 216, 254, 0.5);
    }

    .el-form-item {
      margin-bottom: 25px;
    }

    .el-input {
      ::v-deep .el-input__inner {
        background-color: rgba(255, 255, 255, 0.1);
        border: 1px solid rgba(104, 216, 254, 0.3);
        color: #d3d6dd;

        &:focus {
          border-color: rgba(104, 216, 254, 0.8);
        }
      }

      ::v-deep .el-input__prefix {
        color: rgba(104, 216, 254, 0.8);
      }
    }

    .login-tips {
      margin-top: 20px;
      color: #d3d6dd;
      font-size: 14px;
      text-align: center;
    }
  }
}
</style>
